package com.pur.zsms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.vo.CommonType;
import com.pur.zsms.dao.ICashierDao;
import com.pur.zsms.dao.IDistributorDao;
import com.pur.zsms.documentsmanage.dao.IDomDao;
import com.pur.zsms.documentsmanage.po.DomPo;
import com.pur.zsms.foodmanage.food.dao.IFoodDao;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.OrderDistributorMaterRealVo;
import com.pur.zsms.ordercustommanage.po.OrderDistributorMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderDistributorPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderDistributorVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.po.CashierOrderPo;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.vo.CashierOrderDetailVo;
import com.pur.zsms.vo.CashierOrderVo;

@Repository("cashierDao")
public class CashierDaoImpl extends GenericDao implements ICashierDao {
	public PaginationSupport cashierListForShop(PageController pageController, CashierOrderVo vo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.cashier_id,o.shop_id,(SELECT a.shop_name FROM t_shop a WHERE a.shop_id = o.shop_id ),o.user_id,(SELECT b.real_name FROM t_user b WHERE b.user_id = o.user_id),o.order_value,o.pay_way,o.submit_time FROM c_cashier_order o ");

		ql.append(" order by o.cashier_id desc");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public List<CashierOrderPo> getCashierOrderPo(CashierOrderVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o ");
		ql.append(" from CashierOrderPo o where 1=1 ");
		if (c.getCashierId() != 0) {
			ql.append(" AND o.cashierId = '" + c.getCashierId() + "'");
		}
		List<CashierOrderPo> list = template.find(ql.toString(), null);
		return list;
	}

	public PaginationSupport cashierListDetailForShop(PageController pageController, CashierOrderDetailVo vo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.cashier_detail_id,o.cashier_id,o.food_id,(SELECT f.food_name FROM t_food f WHERE f.food_id = o.food_id ),o.food_number,o.food_value FROM c_cashier_order_detail o WHERE o.cashier_id = '"
				+ vo.getCashierId() + "'");

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findFoodCashier(PageController pageController, FoodVo vo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT  a.food_id,a.`food_name`,a.`food_value`,a.`food_type`,b.`food_type_name`,a.`is_active` FROM t_food a ,t_food_type b WHERE  1=1 AND a.shop_id = b.`shop_id` AND a.shop_id ='"
				+ vo.getShopId() + "' AND a.`food_type` =b.`food_type_id` ORDER BY a.`food_type`");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport cashierListStatisticsForShop(PageController pageController, CashierOrderDetailVo vo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.cashier_detail_id, o.cashier_id, o.food_id, (SELECT f.food_name FROM t_food f WHERE f.food_id = o.food_id AND f.shop_id = c.shop_id ), SUM(o.food_number),SUM(o.food_value*o.food_number),(SELECT ft.food_type_name FROM t_food_type ft WHERE ft.food_type_id = (SELECT f2.food_type FROM t_food f2 WHERE f2.food_id = o.`food_id` ) ) FROM c_cashier_order_detail o , c_cashier_order c WHERE 1=1 AND c.cashier_id= o.cashier_id AND c.shop_id = '"
				+ vo.getShopId() + "'");
		if (vo.getPayWay() != 0) {
			ql.append(" AND c.pay_way = '" + vo.getPayWay() + "'");
		}
		if (vo.getDateStart() != null && !"".equals(vo.getDateStart())) {
			ql.append(" AND c.submit_time >= '" + vo.getDateStart() + "'");
		}
		if (vo.getDateEnd() != null && !"".equals(vo.getDateEnd())) {
			ql.append(" AND c.submit_time <= '" + vo.getDateEnd() + "'");
		}

		if (vo.getFoodType() != 0 || (vo.getFoodName() != null && !"".equals(vo.getFoodName()))) {
			ql.append(" AND EXISTS ( SELECT 1 FROM t_food f WHERE  o.food_id = f.food_id ");
			if (vo.getFoodName() != null && !"".equals(vo.getFoodName())) {
				ql.append(" AND f.food_name LIKE '%" + vo.getFoodName() + "%'");
			}
			if (vo.getFoodType() != 0) {
				ql.append(" AND f.food_type = '" + vo.getFoodType() + "' ");
			}
			ql.append(")");
		}
		ql.append(" GROUP BY o.food_id");

		return this.findByNativeQuery(ql.toString(), pageController);
	}
	
	public PaginationSupport cashierListStatisticsDetailForShop(PageController pageController, CashierOrderDetailVo vo) throws GenericException{
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.cashier_detail_id, o.cashier_id, o.food_id, (SELECT f.food_name FROM t_food f WHERE f.food_id = o.food_id AND f.shop_id = c.shop_id ), o.food_number,o.food_value,o.food_value * o.food_number,c.submit_time FROM c_cashier_order_detail o , c_cashier_order c WHERE 1=1 AND c.cashier_id= o.cashier_id AND c.shop_id = '"
				+ vo.getShopId() + "'");
		ql.append(" AND o.food_id = '" + vo.getFoodId() + "'");
		if (vo.getPayWay() != 0) {
			ql.append(" AND c.pay_way = '" + vo.getPayWay() + "'");
		}
		if (vo.getDateStart() != null && !"".equals(vo.getDateStart())) {
			ql.append(" AND c.submit_time >= '" + vo.getDateStart() + "'");
		}
		if (vo.getDateEnd() != null && !"".equals(vo.getDateEnd())) {
			ql.append(" AND c.submit_time <= '" + vo.getDateEnd() + "'");
		}

		if (vo.getFoodType() != 0 || (vo.getFoodName() != null && !"".equals(vo.getFoodName()))) {
			ql.append(" AND EXISTS ( SELECT 1 FROM t_food f WHERE  o.food_id = f.food_id ");
			if (vo.getFoodName() != null && !"".equals(vo.getFoodName())) {
				ql.append(" AND f.food_name LIKE '%" + vo.getFoodName() + "%'");
			}
			if (vo.getFoodType() != 0) {
				ql.append(" AND f.food_type = '" + vo.getFoodType() + "' ");
			}
			ql.append(")");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public void deleteFoodCashier(FoodVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" delete FoodPo o where o.foodId='" + c.getFoodId() + "'");
		template.executeUpdate(ql.toString());
	}

}
